package com.it.hanzhongtravelv1.controller;

import com.it.hanzhongtravelv1.dto.FAQDTO;
import com.it.hanzhongtravelv1.entity.FAQ;
import com.it.hanzhongtravelv1.service.FAQService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/faqs")
@CrossOrigin(origins = "*")
public class FAQController {
    
    @Autowired
    private FAQService faqService;
    
    /**
     * 获取所有FAQ
     */
    @GetMapping
    public ResponseEntity<List<FAQDTO>> getAllFAQs() {
        List<FAQDTO> faqs = faqService.getAllFAQs();
        return ResponseEntity.ok(faqs);
    }
    
    /**
     * 根据分类获取FAQ
     */
    @GetMapping("/category/{category}")
    public ResponseEntity<List<FAQDTO>> getFAQsByCategory(@PathVariable String category) {
        List<FAQDTO> faqs = faqService.getFAQsByCategory(category);
        return ResponseEntity.ok(faqs);
    }
    
    /**
     * 搜索FAQ
     */
    @GetMapping("/search")
    public ResponseEntity<List<FAQDTO>> searchFAQs(@RequestParam String keyword) {
        List<FAQDTO> faqs = faqService.searchFAQs(keyword);
        return ResponseEntity.ok(faqs);
    }

    /**
     * 创建FAQ
     */
    @PostMapping
    public ResponseEntity<FAQDTO> createFAQ(@RequestBody FAQ faq) {
        return ResponseEntity.ok(faqService.saveFAQ(faq));
    }

    /**
     * 更新FAQ
     */
    @PutMapping("/{id}")
    public ResponseEntity<FAQDTO> updateFAQ(@PathVariable Long id, @RequestBody FAQ faq) {
        faq.setId(id);
        return ResponseEntity.ok(faqService.saveFAQ(faq));
    }

    /**
     * 删除FAQ
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteFAQ(@PathVariable Long id) {
        faqService.deleteFAQ(id);
        return ResponseEntity.noContent().build();
    }
} 